package com.thothsun.fwd;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Button;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intent = new Intent(MainActivity.this, FxService.class);

        findViewById(R.id.btn_open).setOnClickListener(v -> {
            if (hasPermission()) {
                startService(intent);
            } else {
                Intent intentPermission = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intentPermission.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intentPermission, 100);
            }
        });

        findViewById(R.id.btn_close).setOnClickListener(v -> {
            stopService(intent);
        });


    }

    private boolean hasPermission() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { //4.4之前
            return true;
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { //4.4-6.0
            try {
                Class cls = Class.forName("android.content.Context");
                Field declaredField = cls.getDeclaredField("APP_OPS_SERVICE");
                declaredField.setAccessible(true);
                Object obj = declaredField.get(cls);
                if (!(obj instanceof String)) {
                    return false;
                }
                String str = (String) obj;
                obj = cls.getMethod("getSystemService", String.class).invoke(this, str);
                cls = Class.forName("android.app.AppOpsManager");
                Field declaredField2 = cls.getDeclaredField("MODE_ALLOWED");
                declaredField2.setAccessible(true);
                Method checkOP = cls.getMethod("checkOp", Integer.TYPE, Integer.TYPE, String.class);
                int result = (Integer) checkOP.invoke(obj, 24, Binder.getCallingUid(), getPackageName());
                return result == declaredField2.getInt(cls);
            } catch (Exception e) {
                return false;
            }
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { //6.0-8.0
            return Settings.canDrawOverlays(this);
        } else { //8.0之后
            AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
            if (appOpsManager == null) {
                return false;
            }
            int mode = appOpsManager.checkOpNoThrow("android:system_alert_window", android.os.Process.myUid(), getPackageName());
            return Settings.canDrawOverlays(this) || mode == AppOpsManager.MODE_ALLOWED || mode == AppOpsManager.MODE_IGNORED;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && hasPermission()) {
            startService(intent);
        }
    }
}